package com.atguigu.gmall.pms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.CategoryMapper;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryCategoriesById(Long id) {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        if(id != -1){
            queryWrapper.eq("parent_id", id);
        }

        //List<CategoryEntity> categoryEntities = baseMapper.selectList(queryWrapper);
        return this.categoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<CategoryEntity> queryLvl23CateriesByPid(Long pid) {
        return categoryMapper.queryCategoriesByPid(pid);
    }

    @Override
    public List<CategoryEntity> queryLvl123CatesByCid3(Long cid3) {
        //根据三级分类ID查询三级分类
        CategoryEntity categoryEntity3 = this.getById(cid3);
        if (categoryEntity3 == null){
            return null;
        }

        //根据二级分类的Id查询二级分类  三级分类的父ID就是二级分类的ID
        CategoryEntity categoryEntity2 = this.getById(categoryEntity3.getParentId());

        //根据一级分类的Id查询一级分类
        CategoryEntity categoryEntity1 = this.getById(categoryEntity2.getParentId());

        return Arrays.asList(categoryEntity1,categoryEntity2,categoryEntity3);
    }

}